package com.xueyu.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xueyu.dao.privilegesDao;
import com.xueyu.dao.rpDao;
import com.xueyu.domain.privileges;
import com.xueyu.domain.rp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/privileges")
public class privilegesController {
    @Autowired //系统权限
    private privilegesDao privilegesDao;

    @Autowired //系统权限
    private rpDao rpDao;


    @RequestMapping("/update/{userId}")
    @ResponseBody
    public Map<String, Object> privileges(@PathVariable("userId") int userId){
        //查询权限的父菜单
        QueryWrapper<privileges> queryWrapper = Wrappers.query();
        queryWrapper.eq("parentid", 0);
        List<privileges> privilegesList = privilegesDao.selectList(queryWrapper);

        //查询权限的子菜单
        QueryWrapper<privileges> queryWrapper2 = Wrappers.query();
        queryWrapper2.ne("parentid", 0);
        List<privileges> privilegesList2 = privilegesDao.selectList(queryWrapper2);


        QueryWrapper<rp> queryWrapper3 = Wrappers.query();
        queryWrapper3.eq("RoleID", userId);
        List<rp> List = rpDao.selectList(queryWrapper3);


        for (privileges parent : privilegesList) {
            List<privileges> children = new ArrayList<>(); // 子菜单数组
            for (privileges child : privilegesList2) {
                if (child.getParentid() == parent.getId()) {
                    children.add(child);
                }
            }
            parent.setChildren(children);
        }






            Map<String, Object> map = new HashMap<>();
            if (privilegesList != null && privilegesList2 != null && !privilegesList.isEmpty() && !privilegesList2.isEmpty()) {
                map.put("code", 200);
                map.put("msg", "查询成功");
                map.put("data", privilegesList);
                map.put("list", List);
            } else {
                map.put("code", 404);
                map.put("msg", "未找到对应数据");
            }

        return map;
    }









    @RequestMapping("/list/{userId}/{data}")
    @ResponseBody
    public Map<String, Object> batchById(@PathVariable("userId") int userId,@PathVariable(value="data",required  = false) int data[]){
        QueryWrapper<rp> queryWrapper1 = new QueryWrapper<rp>();
        queryWrapper1.eq("RoleID",userId);
        rpDao.delete(queryWrapper1);
if(data!= null){
        for(int menuId :data ){
            rp srm = new rp();
            srm.setRoleID(userId);
            srm.setSPID(menuId);
            try {
                rpDao.insert(srm);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
}
        Map<String, Object> map = new HashMap<>();
        return map;
    }

}
